\subsection{GCC---one more thing}
\label{use_parts_of_C_strings}

The fact that an \IT{anonymous} C-string has \IT{const} type (\myref{string_is_const_char}), 
and that C-strings allocated in constants segment are guaranteed to be immutable, has an interesting consequence:
the compiler may use a specific part of the string.

Let's try this example:

\begin{lstlisting}[style=customc]
#include <stdio.h>

int f1()
{
	printf ("world\n");
}

int f2()
{
	printf ("hello world\n");
}

int main()
{
	f1();
	f2();
}
\end{lstlisting}

Common \CCpp{}-compilers (including MSVC) allocate two strings, but let's see what GCC 4.8.1 does:

\begin{lstlisting}[caption=GCC 4.8.1 + IDA listing,style=customasmx86]
f1              proc near

s               = dword ptr -1Ch

                sub     esp, 1Ch
                mov     [esp+1Ch+s], offset s ; "world\n"
                call    _puts
                add     esp, 1Ch
                retn
f1              endp

f2              proc near

s               = dword ptr -1Ch

                sub     esp, 1Ch
                mov     [esp+1Ch+s], offset aHello ; "hello "
                call    _puts
                add     esp, 1Ch
                retn
f2              endp

aHello          db 'hello '
s               db 'world',0xa,0
\end{lstlisting}

Indeed: when we print the \q{hello world} string
these two words are positioned in memory adjacently and \puts called from \GTT{f2()}
function is not aware that this string is divided. 
In fact, it's not divided; it's divided only \q{virtually}, in this listing.

When \puts is called from \GTT{f1()}, it uses the \q{world} string plus a zero byte. \puts is not aware that there is something before this string!

This clever trick is often used by at least GCC and can save some memory.
This is close to \IT{string interning}.

Another related example is here: \myref{strstr_example}.

